package com.sunfeng.linkedlist;

import javax.xml.bind.SchemaOutputResolver;

class A {
    public static void main(String[] args) {
        Node<String> node = new Node<String>();
        node.add("aaaa");
        node.add("bbb");
     //  System.out.println(node.popFirst());
       // System.out.println(node.popFirst());


        System.out.println(node);
    }
}

public class Node<T> {

    private final T value;
    private final Node<T> next;
    private Node<T> head;
    private int size;

    public Node(T value, Node<T> next) {
        this.value = value;
        this.next = next;
    }

    public Node() {
        this.value = null;
        this.next = null;
    }

    public T getValue() {
        return value;
    }

    public Node<T> getNext() {
        return next;
    }





    public boolean isEmpty() {
        return this.head == null;
    }

    public void clear() {
        this.size = 0;
        this.head = null;
    }

    // 向链表的头部添加元素
    public void add(T t) {
        Node<T> node = new Node<T>(t, head);
        this.head = node;
        this.size++;
    }

    public T peekFirst() {
        if (isEmpty()) {
            throw new IndexOutOfBoundsException("队列为空，不支持此方法");
        }
        return head.getValue();
    }

    public T popFirst() {
        if (isEmpty()) {
            throw new IndexOutOfBoundsException("队列为空，不支持此方法");
        }
        final T value = head.getValue();
        this.head = head.getNext();
        this.size--;
        return value;
    }

    public int getSize() {
        return size;
    }

    @Override
    public String toString() {
        Node<T> node = this.head;
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("[");
        while (node != null) {
            stringBuffer.append(node.getValue().toString()).append(",");
            node = node.getNext();
        }
        if (stringBuffer.length() > 1) {
            stringBuffer.deleteCharAt(stringBuffer.length() - 1);
        }
        stringBuffer.append("]");
        return stringBuffer.toString();
    }
}
